package com.snailhd.loan;

import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author hd
 * @description 借贷还款计算
 * @date 2019-09-20 22:12
 */
@Slf4j
public class CalcRepayment {

    public static void main(String[] args) {
        CalcRepayment calcRepay = new CalcRepayment();

        //借贷总金额
        int loan = 90000;
        //日利率
        BigDecimal dayRate = new BigDecimal(0.00045);
        //月还款金额计算样本
        int[] monthRepays = {1500,2000,2500,3000,3500,4000,4500,5000,5500,6000,6500,7000};

        List<CalcResult> results = new ArrayList();
        for(int monthRepay :monthRepays){
            CalcResult calcResult = calcRepay.clac(loan,dayRate,monthRepay);
            results.add(calcResult);
        }

        System.out.println("***************************** result ***********************************");
        results.forEach(e -> {
            System.out.println(e.toString());
        });

    }

    /**
     *
     * @param loan 借款 （元）
     * @param dayRate 日利率
     * @param monthRepay 月还款 （元）
     */
    private CalcResult clac(Integer loan, BigDecimal dayRate,Integer monthRepay){
        BigDecimal arrears = new BigDecimal(loan); //待还款
        BigDecimal mrp = new BigDecimal(monthRepay);//月还款

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,9);
        dayRate = dayRate.add(new BigDecimal(1));

        BigDecimal totalRepay = new BigDecimal(0);
        int monthNum = 0;
        while(arrears.compareTo(mrp)>0){
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            arrears = arrears.multiply(dayRate);
            if (calendar.get(Calendar.DAY_OF_MONTH)==1){
                monthNum++;
                //每月1号
                arrears = arrears.subtract(mrp);
                totalRepay = totalRepay.add(mrp);
                System.out.println("date:"+formatDate(calendar.getTime())+"还款："+monthRepay+", 待还款："+arrears.setScale(2,BigDecimal.ROUND_HALF_UP).toString());
            }
        }
        totalRepay.add(arrears);
        monthNum++;

        int totalRepayInt =  totalRepay.setScale(2,BigDecimal.ROUND_HALF_UP).intValue();

        System.out.println("总还款："+totalRepayInt+",总月数:"+monthNum);
        System.out.println("***********************************************************************************");

        return new CalcResult(monthRepay,totalRepayInt,monthNum);
    }


    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static String formatDate(Date date){
        return sdf.format(date);
    }

    @Data
    class CalcResult{
        private Integer monthRepay;
        private Integer totalRepay;
        private Integer totalMonth;

        public CalcResult(Integer monthRepay, Integer totalRepay, Integer totalMonth) {
            this.monthRepay = monthRepay;
            this.totalRepay = totalRepay;
            this.totalMonth = totalMonth;
        }

        @Override
        public String toString(){
            StringBuilder sb = new StringBuilder();
            sb.append("月供：").append(monthRepay).append(",").append("总还款时间：").append(formatMonth(totalMonth)).append("总还款：").append(totalRepay);
            return sb.toString();
        }

        public String formatMonth(int month){
            StringBuilder sb = new StringBuilder();
            int year = 0;
            if(month>=12){
                year = month/12;
            }
            if(year!=0) sb.append(year).append("年");
            month = month%12;
            if(month!=0) sb.append(month).append("月");
            return sb.toString();
        }

    }

}
